package cn.kgc.yigou.controller;

import cn.kgc.yigou.common.ResultEntity;
import cn.kgc.yigou.pojo.User;
import cn.kgc.yigou.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UserController {
    @Resource
    UserService userService;
    @RequestMapping("/index")
    public String Index(){
        return "index";
    }
    @RequestMapping("/login")
    public String doLogin(User user, Model model, HttpSession session){
        User user1=userService.login(user);
        if (user1!=null){
            if (user1.getRoleid()==1){
               /* session.setAttribute();*/
                return "back/index";
            }else{
                return "index";
            }
        }else{
            model.addAttribute("message","用户名或密码错误");
        }
        return "login1";
    }


    @RequestMapping("/regist")
    public String regist(User user,Model model){
        int regist=userService.save(user);
        if (regist>0){
            model.addAttribute("message","注册成功");
            return "login1";
        }else{
            model.addAttribute("message","注册失败");
            return "zhuce";
        }
    }
    @RequestMapping("/yigou/search/by/keyword")
    @ResponseBody
    public ResultEntity<PageInfo<User>> user(
            @RequestParam(value="pageNum", defaultValue="1",required = false) Integer pageNum,
            @RequestParam(value="pageSize", defaultValue="2",required = false) Integer pageSize,
            @RequestParam(value="keyword", defaultValue="",required = false) String keyword
    ){
        PageInfo<User> pageInfo=userService.queryForKeywordWithPage(pageNum,pageSize,keyword);
        return ResultEntity.successWithoutData(pageInfo);
    }

    ////根据id查询
    @ResponseBody
    @RequestMapping("/user/get/list/by/id/list")
    public ResultEntity<List<User>> getUserListByIdList(@RequestBody List<Integer> userIdList) {

        List<User> userList = userService.getUserListByIdList(userIdList);
        return ResultEntity.successWithoutData(userList);
        //userList查不出来数据service,userIdListyoush
    }

    //删除
    @ResponseBody
    @RequestMapping("/user/batch/remove")
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> userIdList) {
        userService.batchRemove(userIdList);
    return ResultEntity.successWithoutData();
    }
    //修改
    @ResponseBody
    @RequestMapping("/user/update/user")
    public ResultEntity<String> updateUser(User user){
        System.out.println(user);
        userService.updateUser(user);
        return ResultEntity.successWithoutData();
    }

}
